package com.yeyaomai.dksns.flowis.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.yeyaomai.dksns.flowis.domain.FlowAuth;
import com.yeyaomai.dksns.flowis.domain.FlowRole;
import com.yeyaomai.dksns.flowis.service.FlowAuthService;
import com.yeyaomai.dksns.flowis.service.FlowRoleService;
import com.yeyaomai.dksns.flowis.servlet.dto.Auth;

@Controller
@RequestMapping("/rolemanage/*")
public class FlowRoleController {

	@Autowired
	private FlowRoleService flowRoleService;
	
	@Autowired
	private FlowAuthService flowAuthService;

	@RequestMapping(value = "/toaddrole")
	public ModelAndView toAddRole() {
		ModelAndView view = new ModelAndView("add-role");
		List<FlowAuth> flowAuthList = new ArrayList<FlowAuth>();
		flowAuthList = flowAuthService.listFLowAuthByStatus();
		Map<Long,String> map = new HashMap<Long, String>();
		Map<String,String> modelMap = new HashMap<String, String>();
		String html = Auth.getHtml(flowAuthList,map,modelMap);
		view.addObject("authHtml", html);
		return view;
	}

	@RequestMapping(value = "/addrole")
	public String addFlowUser(FlowRole flowRole, HttpServletRequest request) {
		flowRole.setCreateTime(new Date());
		flowRole.setStatus(0);
		flowRoleService.addFlowRole(flowRole);
		return "redirect:/rolemanage/listrole?number=1";
	}

	@RequestMapping(value = "/listrole")
	public ModelAndView listFlowUser(HttpServletRequest request) {

		ModelAndView view = new ModelAndView("rolelist");
		String number = request.getParameter("number");
		Page<FlowRole> result = flowRoleService.listFlowRole(Integer
				.valueOf(number));
		view.addObject("result", result);
		view.addObject("number", number);
		return view;
	}

	@RequestMapping(value = "/delflowrole")
	public String delFlowRole(HttpServletRequest request) {
		String roleId = request.getParameter("roleId");
		flowRoleService.deleteFlowRole(Long.valueOf(roleId));
		return "redirect:/rolemanage/listrole?number=1";
	}

	@RequestMapping(value = "/isexistflowrole")
	public void isExistFlowRole(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String roleName = request.getParameter("roleName");
		boolean isExistFlowRole = flowRoleService.isExistFlowRole(roleName
				.trim());
		String result = "";
		if (isExistFlowRole) {
			result = "0";
		} else {
			result = "1";
		}
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(result);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/toupdateflowrole")
	public ModelAndView toUpdateFlowRole(HttpServletRequest request) {
		String roleId = request.getParameter("roleId");
		FlowRole flowRole = flowRoleService.getFlowRoleByRoleId(Long
				.valueOf(roleId));
		List<FlowAuth> flowAuthList = new ArrayList<FlowAuth>();
		
		flowAuthList = flowAuthService.listFLowAuthByStatus();
		Map<Long,String> map = new HashMap<Long, String>();
		Map<String,String> modelMap = new HashMap<String, String>();
		Map<Integer,Object> totalMap = new HashMap<Integer,Object>();
		totalMap = flowAuthService.getFlowAuthByRoleId(Long.valueOf(roleId));
		map = (Map<Long, String>) totalMap.get(1);
		modelMap = (Map<String, String>) totalMap.get(2);
		String html = Auth.getHtml(flowAuthList,map,modelMap);
		ModelAndView view = new ModelAndView("role_edit");
		view.addObject("authHtml", html);
		view.addObject("flowRole", flowRole);
		return view;
	}
	
	@RequestMapping(value = "/updateflowrole")
	public String updateFlowRole(FlowRole flowRole,HttpServletRequest request) {
		flowRoleService.updateFlowRole(flowRole);
		return "redirect:/rolemanage/listrole?number=1";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/restartCheck")
	public void restartCheck(@RequestParam("roleId") String roleId, HttpServletResponse response) {
		List<FlowAuth> flowAuthList = new ArrayList<FlowAuth>();
		flowAuthList = flowAuthService.listFLowAuthByStatus();
		Map<Long,String> map = new HashMap<Long, String>();
		Map<String,String> modelMap = new HashMap<String, String>();
		Map<Integer,Object> totalMap = new HashMap<Integer,Object>();
		totalMap = flowAuthService.getFlowAuthByRoleId(Long.valueOf(roleId));
		map = (Map<Long, String>) totalMap.get(1);
		modelMap = (Map<String, String>) totalMap.get(2);
		String html = Auth.getHtml(flowAuthList,map,modelMap);
		response.setContentType("text/html;charset=UTF-8");
		try {
			response.getWriter().print(html);
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
	
	
}
